package eproject.mm.com.projectsupervision.activity;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cn.carbs.android.segmentcontrolview.library.SegmentControlView;
import eproject.mm.com.projectsupervision.R;
import eproject.mm.com.projectsupervision.adapter.BaseFragmentPagerAdapter;
import eproject.mm.com.projectsupervision.adapter.ProjectPagerAdapter;
import eproject.mm.com.projectsupervision.fragment.BaseFragment;
import eproject.mm.com.projectsupervision.fragment.LazyFragment;
import eproject.mm.com.projectsupervision.fragment.ProjectInfoFragment;
import eproject.mm.com.projectsupervision.widget.IconFontTextview;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

public class ProjectActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private ArrayList<View> vList;
    private BaseFragmentPagerAdapter baseFragmentPagerAdapter;
    private SegmentControlView topTab;
    private PtrFrameLayout ptrFrameLayout01;

    String prjId,prjName,prjPercentdone;

    private List<LazyFragment> fragmentList;
    IconFontTextview iftv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project);

        Intent intent = getIntent();
        Bundle bundleFragment = intent.getExtras();
        prjId = bundleFragment.getString("prjId");
        prjName = bundleFragment.getString("prjName");
        prjPercentdone = bundleFragment.getString("prjPercentdone");

        topTab = (SegmentControlView)findViewById(R.id.topTab);

        viewPager = (ViewPager) findViewById(R.id.vp_project);
        viewPager.setOffscreenPageLimit(2);
//        vList = new ArrayList<View>();
//        LayoutInflater li = getLayoutInflater();
//        vList.add(li.inflate(R.layout.project_info,null,false));
//        vList.add(li.inflate(R.layout.project_report,null,false));
//        vList.add(li.inflate(R.layout.project_problem,null,false));

        fragmentList = new ArrayList<LazyFragment>();
        LazyFragment base01 = new ProjectInfoFragment();
        LazyFragment base02 = new BaseFragment();
        LazyFragment base03 = new BaseFragment();

        Bundle bundle1 = new Bundle();
        bundle1.putString("type", "info");
        bundle1.putString("projectId", prjId);
        bundle1.putString("projectName", prjName);
        bundle1.putString("prjPercentdone", prjPercentdone);

        base01.setArguments(bundle1);

        Bundle bundle2 = new Bundle();
        bundle2.putString("type", "plan");
        bundle2.putString("notclick", "1");
        bundle2.putString("projectId", prjId);
        base02.setArguments(bundle2);

        Bundle bundle3 = new Bundle();
        bundle3.putString("type", "problem");
        bundle3.putString("projectId", prjId);
        base03.setArguments(bundle3);

        fragmentList.add(base01);
        fragmentList.add(base02);
        fragmentList.add(base03);

        FragmentManager fm = getSupportFragmentManager();
        baseFragmentPagerAdapter = new BaseFragmentPagerAdapter(fm, fragmentList);
        viewPager.setAdapter(baseFragmentPagerAdapter);
        viewPager.setCurrentItem(0);

        //topTab.setViewPager(viewPager);
        //ptrFrameLayout01 = (PtrFrameLayout)vList.get(0).findViewById(R.id.ptrFL_info);
        initEvent();

        iftv_back = (IconFontTextview)findViewById(R.id.iftv_back);
        iftv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initEvent() {
        topTab.setOnSegmentChangedListener(new SegmentControlView.OnSegmentChangedListener() {
            @Override
            public void onSegmentChanged(int newSelectedIndex) {
                viewPager.setCurrentItem(newSelectedIndex, false);
            }
        });
        topTab.setViewPager(viewPager);


//        ptrFrameLayout01.setPtrHandler(new PtrHandler() {
//            @Override
//            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
//                /**
//                 *检查是否可以刷新，这里使用默认的PtrHandler进行判断
//                 */
//                //return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
//                return true;
//            }
//
//            @Override
//            public void onRefreshBegin(PtrFrameLayout frame) {
//                /**
//                 * 在刷新前需要准备什么工作
//                 */
//                //new MyAsyncTask().execute();
//            }
//        });
    }
}
